using System;
using System.Collections;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Hector.Api;

namespace Me.Prettyprint.Cassandra.Model
{
    /// <summary>
    /// Operation classes extend this abstract class, so that we can use
    /// lambda expressions to execute queries. Subclasses need to implement
    /// public abstract T DoInKeyspace(KeyspaceService ks) and provide
    /// the logic.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class KeyspaceOperationCallback<T>
	{
		public KeyspaceOperationCallback()
		{
		}
        public abstract T DoInKeyspace(KeyspaceService ks);
        public ExecutionResult<T> DoInKeyspaceAndMeasure(KeyspaceService ks)
        {
            long startTicks = DateTime.Now.Ticks;
            T value = default(T);
            value = DoInKeyspace(ks);
            long elapsed = DateTime.Now.Ticks - startTicks;
            return new ExecutionResult<T>(value,elapsed,ks.CassandraHost);
        }
	}

    public class KeyspaceOperationCallbackBuilder<T> : KeyspaceOperationCallback<T>
    {
        Func<KeyspaceService, T> act;

        public KeyspaceOperationCallbackBuilder(Func<KeyspaceService, T> act)
            : base()
        {
            this.act = act;
        }

        public override T DoInKeyspace(KeyspaceService ks)
        {
            return act(ks);
        }
    }
}